import { defineStore } from "pinia";
import { ThemeEnum } from "@/enums/appEnum";
import { APP_DARK_MODE_KEY, APP_MENU_COLLAPSE_KEY } from "@/enums/cacheEnum";

import { createLocalStorage } from "@/utils/cache";
let ls = createLocalStorage();

export const useConfig = defineStore(`app`, {
  state: () => {
    return {
      isCollapse: ls.getItem(APP_MENU_COLLAPSE_KEY) || false,
      darkMode: ls.getItem(APP_DARK_MODE_KEY) || ThemeEnum.DARK
    };
  },
  getters: {
    getCollapsed(state) {
      return state.isCollapse;
    },
    getDarkMode(state) {
      return state.darkMode;
    }
  },
  actions: {
    /**
     * 菜单栏折叠
     * @param {Boolean} payload 折叠
     */
    setMenuCollapse(payload) {
      this.isCollapse = payload;
      ls.setItem(APP_MENU_COLLAPSE_KEY, payload);
    },
    /**
     * 设置黑暗主题
     * @param {string} mode 主题类型： right | black
     */
    setDarkMode(payload) {
      this.darkMode = payload;
      ls.setItem(APP_DARK_MODE_KEY, payload);
    }
  }
});
